package com.panvan.android;

import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Looper;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.panvan.android.BuildConfig;

import java.util.Timer;
import java.util.TimerTask;


public class WebViewClientListener extends WebViewClient {

    private static final int TIME_OUT_PROGRESS = 30;
    private static final int TIME_OUT_MILLIS = 10*1000;
    private Handler handler;
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
//        if (handler == null) {
//            handler = new Handler(Looper.getMainLooper());
//        }
//        handler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                if (view.getProgress() < TIME_OUT_PROGRESS) {
//                    view.loadUrl(getErrorUrlString(408, "Connection time out " + TIME_OUT_MILLIS + ". current progress is: " + view.getProgress(), "", url));
//                }
//            }
//        }, TIME_OUT_MILLIS);
    }

    @Override
    public void onLoadResource(WebView view, String url) {
        super.onLoadResource(view, url);
//        if (handler == null) {
//            handler = new Handler(Looper.getMainLooper());
//        }
//        handler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                if (view.getProgress() < TIME_OUT_PROGRESS) {
//                    view.loadUrl(getErrorUrlString(408, "connection time out " + TIME_OUT_MILLIS + ". current progress is: " + view.getProgress(), "", url));
//                }
//            }
//        }, TIME_OUT_MILLIS);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
    }


    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
         super.shouldOverrideUrlLoading(view, request);
         return true;
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        //用javascript自定义错误界面
//        view.loadUrl(getErrorUrlString(errorCode,description, "",failingUrl));
        super.onReceivedError(view,errorCode,description,failingUrl);
    }
    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        //用javascript自定义错误界面
//        view.loadUrl(getErrorUrlString(error.getErrorCode(),error.getDescription().toString(), request.getMethod(),request.getUrl().toString()));
        super.onReceivedError(view,request,error);
    }
    @Override
    public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
        //用javascript自定义错误界面
       //view.loadUrl(getErrorUrlString(errorResponse.getStatusCode(),"出现错误", request.getMethod(),request.getUrl().toString()));
        super.onReceivedHttpError(view,request,errorResponse);
    }

    protected String getErrorUrlString(int errorCode, String description,String method,String url){
        return BuildConfig.ERROR_PATH+"?"+
                "errorCode="+errorCode+
                "&errorDescription="+description+
                "&method="+method+
                "&url="+ url+
                "&mainUrl="+ BuildConfig.MAIN_URL
                .replaceAll("[&]","%26")
                .replaceAll("[=]","%3D")
                .replaceAll("[?]","%3F")
                .replaceAll("[#]","%23");
    }
    //        shouldOverrideUrlLoading(WebView view, String url);
//        //在网页上的所有加载都经过这个方法,这个函数我们可以做很多操作。
//        //比如获取url，查看url.contains(“add”)，进行添加操作
//        shouldOverrideKeyEvent(WebView view, KeyEvent event);
//        //重写此方法才能够处理在浏览器中的按键事件。
//        onPageStarted(WebView view, String url, Bitmap favicon) ;
//        //这个事件就是开始载入页面调用的，我们可以设定一个loading的页面，告诉用户程序在等待网络响应。
//        onPageFinished(WebView view, String url);
//        //在页面加载结束时调用。同样道理，我们可以关闭loading 条，切换程序动作。
//        onLoadResource(WebView view, String url) ;
//        // 在加载页面资源时会调用，每一个资源（比如图片）的加载都会调用一次。
//        onReceivedError(WebView view, int errorCode, String description, String failingUrl);
//        // (报告错误信息)
//        doUpdateVisitedHistory(WebView view, String url, boolean isReload);
//        //(更新历史记录)
//        onFormResubmission(WebView view, Message dontResend, Message resend);
//        //(应用程序重新请求网页数据)
//        onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host,String realm);
//        //（获取返回信息授权请求）
//        onReceivedSslError(WebView view, SslErrorHandler handler, SslError error);
//        //重写此方法可以让webview处理https请求。
//        onScaleChanged(WebView view, float oldScale, float newScale);
//        // (WebView发生改变时调用)
//        onUnhandledKeyEvent(WebView view, KeyEvent event);
//        //（Key事件未被加载时调用）
//    }
}
